﻿using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UI;
using UnityEngine;
using System.Linq;
using UnityEngine.UI;
using Base.Extensions;
using System.Threading;

public class FbxListPanel : BasePanel
{
    private CanvasGroup canvasGroup;//获取CanvasGroup组件，用于控制该面板的交互功能
    private Dictionary<string, string> dic;
    private Transform cont;
    private Transform FbxCont;
    string text;
    // Use this for initialization
    void Awake () {
        canvasGroup = GetComponent<CanvasGroup>();
        dic = Base.Data.Dictionaries.Fbxmodel;
        cont = transform.Find("Viewport/Content");
        text = "UIItem/File";
        FbxCont = GameObject.Find("Object").transform;
    }

    public override void OnEnter()
    {
        canvasGroup.blocksRaycasts = true;
        canvasGroup.DOFade(1, 0.6f);
        transform.DOMoveX(0, 0.6F);

    }
    private void Start()
    {
        foreach (var item in dic.Keys)
        {
            GameObject obj = Instantiate(Resources.Load(text)) as GameObject;
            obj.transform.parent = cont;
            obj.transform.localPosition = Vector3.zero;
            Text _text = obj.transform.Find("Text").GetComponent<Text>();
            _text.text = item;
            Button button = obj.transform.Find("Button").GetComponent<Button>();
            Text butext = button.transform.Find("Text").GetComponent<Text>();
            butext.text = "加载";
            button.onClick.AddListener(delegate () { OnClick(item, butext); });
        }

    }
    public override void OnExit()
    {
        canvasGroup.blocksRaycasts = false;
        canvasGroup.DOFade(0, 0.6f);
        transform.DOMoveX(-200, 0.6F);
    }
    // Update is called once per frame
    void Update () {
    }
    
    public void OffFBX()
    {
        UIManager.Instance.PopPanel();
    }
    public void OnClick(string key,Text but)
    {
        if (but.text == "加载")
        {
            but.text = "卸载";
            ShowThread(key);
            //StartCoroutine(ShowThread(key));

        }
        else
        {
            but.text = "加载";
        }
    }
    int index = 0;
    /*IEnumerator*/
    void ShowThread(string key)
    {
        string path;
        dic.TryGetValue(key, out path);
        GameObject obj = Instantiate(Resources.Load(path)) as GameObject;
        Transform[] form= obj.GetComponentsInChildren<Transform>();
        foreach (var item in form)
        {
            if (item.GetComponent<MeshFilter>() != null)
            {
                //item.gameObject.AddComponent<Base.Custom.SingleObject>().SetSingleObject(item.gameObject);
            }
        }


        //Transform form = obj.transform;
        //AddMeshCollider(obj.transform, key);
        //List<Transform> forms = new List<Transform>();
        //AddMeshCollider(obj.transform, ref forms);
        //AddMeshCollider(forms, key);
        //string path;
        //dic.TryGetValue(key, out path);
        //GameObject obj = Instantiate(Resources.Load(path)) as GameObject;
        //yield return 0;
    }


    private Transform AddMeshCollider(Transform trans,string key)
    { 
        foreach (Transform child in trans)
        {
            if (child.GetComponent<MeshFilter>() != null)
            {
                //child.gameObject.AddComponent<Base.Custom.SingleObject>().SetSingleObject(child.gameObject);
            }
            if (child.childCount != 0)
            {
                Transform _trans = AddMeshCollider(child, key);
                if (_trans != null)
                {
                    return _trans;
                }
            }
        }
        return null;
    }
    private void AddMeshCollider(List<Transform> trans, string key)
    {
        foreach (Transform child in trans)
        {
            List<Base.Custom.SingleObject> sobjs = new List<Base.Custom.SingleObject>();
            Base.Custom.SingleObject sobj = new Base.Custom.SingleObject();
            GameObject form = child.gameObject;
            string _name = child.name;
            //sobj.setSingleObject(form, key);
        }
       
    }
    private Transform AddMeshCollider(Transform trans,ref List<Transform> forms)
    {
        foreach (Transform child in trans)
        {
            if (child.GetComponent<MeshFilter>() != null) 
                forms.Add(child);
            if (child.childCount != 0)
            {
                Transform _trans = AddMeshCollider(child,ref forms);
                if (_trans != null)
                {
                    return _trans;
                }
            }
        }
        return null;
    }
    
}
